import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;

public class MapPanel extends JPanel {
    private final MapService mapService;
    private BufferedImage buffer;

    // 颜色常量
    private static final Color LIGHT_COLOR = Color.WHITE;
    private static final Color DARK_COLOR = Color.DARK_GRAY;
    private static final Color OBSTACLE_COLOR = Color.BLACK;
    private static final Color CAR_COLOR = Color.GREEN;

    public MapPanel(MapService service) {
        this.mapService = service;
        setDoubleBuffered(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (buffer == null || buffer.getWidth() != getWidth() || buffer.getHeight() != getHeight()) {
            buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
        }

        Graphics2D g2d = buffer.createGraphics();
        drawGrid(g2d);
        g.drawImage(buffer, 0, 0, null);
        g2d.dispose();
    }

    private void drawGrid(Graphics2D g) {
        RedisRepository.MapData data = mapService.getCurrentMapData();
        if (data.width <= 0 || data.height <= 0) return;

        int width = data.width;
        int height = data.height;

        // 计算正方形格子尺寸
        int panelWidth = getWidth();
        int panelHeight = getHeight();
        int cellSize = Math.min(panelWidth / width, panelHeight / height);

        // 计算边距使地图居中
        int offsetX = (panelWidth - width * cellSize) / 2;
        int offsetY = (panelHeight - height * cellSize) / 2;

        HashMap<String, Boolean> carMap = data.CarMap;
        HashMap<String,Integer>CarId=data.CarId;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int screenX = offsetX + x * cellSize;
                int screenY = offsetY + y * cellSize;
                String posKey = x + " " + y;

                // 使用同一个 MapData 实例进行判断
                boolean isLighted = mapService.isLighted(x, y, data);
                boolean isObstacle = mapService.isObstacle(x, y, data);
                boolean isCarHere = carMap != null && carMap.containsKey(posKey);

                if (!isLighted) {
                    // 未点亮区域 - 黑色
                    g.setColor(Color.BLACK);
                    g.fillRect(screenX, screenY, cellSize, cellSize);
                } else {
                    if (isCarHere) {
                        // 小车 - 蓝色圆形
                        g.setColor(Color.BLUE);
                        g.fillOval(screenX, screenY, cellSize, cellSize);

                        // 添加小车ID（黄色）
                        g.setColor(Color.YELLOW);
                        g.setFont(new Font("Arial", Font.BOLD, cellSize/2));
                        String carid = String.valueOf(CarId.get(posKey)); // 这里需要根据实际情况获取正确的车辆ID
                        FontMetrics fm = g.getFontMetrics();
                        int textX = screenX + (cellSize - fm.stringWidth(carid)) / 2;
                        int textY = screenY + ((cellSize - fm.getHeight()) / 2) + fm.getAscent();
                        g.drawString(carid, textX, textY);
                    } else if (isObstacle) {
                        // 障碍物 - 红色
                        g.setColor(Color.RED);
                        g.fillRect(screenX, screenY, cellSize, cellSize);
                    } else {
                        // 点亮区域 - 浅灰色
                        g.setColor(Color.LIGHT_GRAY);
                        g.fillRect(screenX, screenY, cellSize, cellSize);
                    }
                }

                // 边框 - 深灰色
                g.setColor(Color.DARK_GRAY);
                g.drawRect(screenX, screenY, cellSize, cellSize);
            }
        }
    }









}







